package com.bkgy.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/***
 * 时间工具类
 *
 * @author cc
 *
 */
public class DateUtil {
    /***
     * 根据格式，把当前系统时间转换成string类型
     *
     * @param format 字符串的时间格式，例如：yyyy-MM-dd HH:mm:ss
     */
    public static String getDateToString(String format) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /***
     * 根据格式，把参数时间转换成string类型
     *
     * @param format 字符串的时间格式，例如：yyyy-MM-dd HH:mm:ss
     */
    public static String getDateToString(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /***
     * 根据格式，把string类型时间转换成Date类型
     *
     * @param format   字符串的时间格式，例如：yyyy-MM-dd HH:mm:ss
     * @param str_date 字符串时间
     */
    public static Date getStringToDate(String format, String str_date) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.parse(str_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 获取系统时间前一天的时间
     *
     */
    public static Date getBeforDate() {
        Date today = new Date();// 获取今天的日期
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);
        return c.getTime();
    }

    /***
     * 获取系统时间后一天的时间
     *
     */
    public static Date getAfterDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, 1);
        Date tomorrow = c.getTime();// 这是明天
        c.setTime(date);
        return tomorrow;
    }

    /***
     * 把时间格式-date"Thu Oct 17 00:51:04 CST 2024"转化为 2024-10-18 11:48:30     ("yyyy-MM-dd HH:mm:ss")
     *
     */
    public static String getdatetostring(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化规则
      /*  long timestamp = date.getTime();
      // 将时间戳转换为日期
        Date date1 = new Date(timestamp);
        // 将日期转换为字符串
        String dateString = sdf.format(date1);*/
        String dateString1 = sdf.format(date);
        return dateString1;
    }

    /**
     * 上一日零时
     *
     * @return
     */
    public static Date getLastDayZero(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, -1);
        String d = getDateToString(c.getTime(), "yyyy-MM-dd 00:00:00");
        return getStringToDate("yyyy-MM-dd 00:00:00", d);
    }

    /**
     * 当日0时
     *
     * @return
     */
    public static Date getCurrentZero(Date date) {
        String d = getDateToString(date, "yyyy-MM-dd 00:00:00");
        return getStringToDate("yyyy-MM-dd 00:00:00", d);
    }

    /**
     * 下一日零时
     *
     * @return
     */
    public static Date getNextDayZero(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, 1);
        String d = getDateToString(c.getTime(), "yyyy-MM-dd 00:00:00");
        return getStringToDate("yyyy-MM-dd 00:00:00", d);
    }

    /**
     * 上月第一天
     */
    public static String lastMonthFirstDay(String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        return DateUtil.getDateToString(c.getTime(), format);
    }

    /**
     * 本周第一天
     */
    public static String thisWeekFirstDay(String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return DateUtil.getDateToString(cal.getTime(), format);
    }

    /**
     * 本月第一天
     */
    public static String thisMonthFirstDay(String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.getDateToString(cal.getTime(), format);
    }

    /**
     * 下月第一天
     */
    public static String nextMonthFirstDay(String format) {
        if (format == null) format = "yyyyMMdd";
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.MONTH, 1);
        return DateUtil.getDateToString(c.getTime(), format);
    }

    /**
     * 1小时前的时间
     */
    public static Date getBeforeHour() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.HOUR_OF_DAY, -1);
        return cal.getTime();
    }

    //获取前一个小时段起点时间
    public static String getBeforeHourStart(Date date) {
        String d2 = getDateToString(date, "yyyyMMddHH");
        Date d = getStringToDate("yyyyMMddHH", d2);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.HOUR_OF_DAY, -1);
        return getDateToString(cal.getTime(), "yyyyMMddHH");
    }

    //时间的下一小时时间点
    public static Date getAfterHour(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY, 1);
        return c.getTime();
    }

    /**
     * 当前班组开始时间点
     */
    public static Date getCurrentCrewStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour >= 8 && hour < 19) {
            cal.set(Calendar.HOUR_OF_DAY, 8);
        } else {
            cal.set(Calendar.HOUR_OF_DAY, 19);
            //8点前属于夜班
            if (hour < 8) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
        }

        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    /**
     * 将日期字符串格式转换
     *
     * @param date       日期格式字符串
     * @param fromFormat 原始格式
     * @param toFormat   目标格式
     */
    public static String tranceDateFormat(String date, String fromFormat, String toFormat) {
        Date d1 = getStringToDate(fromFormat, date);
        return getDateToString(d1, toFormat);
    }

    /**
     * 获取一个周范围的字符串数组
     *
     * @param weeks
     */
    public static String[] getWeekRankArr(int weeks) {
        String[] weekArr = new String[weeks];

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date monday = cal.getTime();//本周周一
        cal.setTime(monday);
        cal.add(Calendar.DAY_OF_MONTH, -7 * (weeks-1));
        for (int i = 0; i < weeks-1; i++) {
            String x1 = DateUtil.getDateToString(cal.getTime(), "MM/dd");
            cal.add(Calendar.DAY_OF_MONTH, 6);
            String x2 = DateUtil.getDateToString(cal.getTime(), "MM/dd");
            cal.add(Calendar.DAY_OF_MONTH, 1);
            weekArr[i] = x1 + "-" + x2;
        }
        weekArr[weeks-1] = DateUtil.getDateToString(monday, "MM/dd")+"-今";
        return weekArr;
    }
}
